<?php

namespace app\common\model\mall\order;

use think\Model;

class Complain extends Model
{
    // 表名
    protected $name = 'mall_order_complain';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'complainType_text',
        'complainStatus_text',
        'complain_target_id_text',
        'respond_target_id_text'
    ];

    public function getComplainTargetIdTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['complainTargetId']) ? $data['complainTargetId'] : '');
        $name = "";
        if($value){
            $name = \app\common\model\User::where("id","=",$value)->field("nickname,username")->find();
            $name = $name["nickname"]!=""?$name["nickname"]:$name["username"];
        }
        return $name;
    }

    public function getRespondTargetIdTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['respondTargetId']) ? $data['respondTargetId'] : '');
        $name = "";
        if($value){
            $name = \app\common\model\mall\shop\Shop::getFieldById($value,"name");
        }
        return $name;
    }

    public function getComplaintypeList()
    {
        return ['1' => __('Complaintype 1'),'2' => __('Complaintype 2'),'3' => __('Complaintype 3'),'4' => __('Complaintype 4')];
    }

    public function getComplainstatusList()
    {
        return ['0' => __('Complainstatus 0'),'1' => __('Complainstatus 1'),'2' => __('Complainstatus 2'),'3' => __('Complainstatus 3'),'4' => __('Complainstatus 4')];
    }

    public function getComplaintypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['complainType']) ? $data['complainType'] : '');
        $list = $this->getComplaintypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getComplainstatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['complainStatus']) ? $data['complainStatus'] : '');
        $list = $this->getComplainstatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 获取订单信息
     */
    public function getOrderInfo($order_id){
        $userId = (int)cookie('uid');

        //判断是否提交过投诉
        $rs = $this->alreadyComplain($order_id,$userId);
        $data = array('complainStatus'=>1);
        if($rs['id']==''){
            //获取订单信息
            $order = db('mall_order_order')->alias('o')
                ->join('__MALL_SHOP_SHOP__ s','o.shop_id=s.id','left')
                ->field('o.shop_id,o.realTotalMoney,o.orderNo,o.id,o.user_id,o.createtime,o.deliverMoney,s.name')
                ->where(['o.id' => $order_id,'o.user_id' => $userId])
                ->find();
            if($order){
                //获取相关商品
                $goods = $this->getOrderGoods($order_id);
                $order["goodsList"] = $goods;
            }
            $data['order'] = $order;
            $data['complainStatus'] = 0;
        }
        return $data;
    }

    // 判断是否已经投诉过
    public function alreadyComplain($orderId,$userId){
        return $this->field('id')->where("order_id=$orderId and complainTargetId=$userId")->find();
    }
    //获取相关商品
    public function getOrderGoods($order_id){
        return db('mall_goods_goods')->alias('g')
            ->field('og.order_id, og.id,g.goodssn, og.goodsname , og.goodsPrice shopPrice,og.goodsimg')
            ->join('__MALL_ORDER_GOODS__ og','g.id = og.goods_id','inner')
            ->where("og.order_id",$order_id)
            ->select();
    }

    /**
     * 获取投诉详情
     * $userType 0买方 >0卖方
     */
    public function getComplainDetail($userType = 0,$sId = 0){
        $userId = (int)cookie('uid');
        $shopId = ($sId==0)?(int)cookie('shop_id'):$sId;
        $id = (int)Input('ids');
        if($userType==0){
            $where['complainTargetId']=$userId;
        }else{
            $where['needRespond']=1;
            $where['respondTargetId']=$shopId;
        }

        //获取订单信息
        $where['oc.id']=$id;
        $rs = $this->alias('oc')
            ->join('__MALL_ORDER_ORDER__ o','oc.order_id=o.id','inner')
            ->join('__MALL_SHOP_SHOP__ s','o.shop_id=s.id')
            ->field('oc.*,o.user_id,o.shop_id,o.realTotalMoney,o.orderNo,o.createtime,o.deliverMoney,s.name')
            ->where($where)->find();
        if($rs){
            //获取相关商品
            $goods = $this->getOrderGoods($rs['order_id']);
            $rs["goodsList"] = $goods;
        }
        return $rs;
    }

}
